
/*
    file:       timers.c

    purpose:    timer routines and ISR

    revisions
    000     RLL     2011-03-12  created file

*/

// ---------------------------------------------------------

#include "timers.h"
#include "main.h"


// ---------------------------------------------------------
void timer_init()
{
    // set up Timer1 as the main timer, with 1ms overflow interrupt
    // with 1 MHz cpu clk, and timer prescale of 1/8, overflow ct is 125
    // CTC mode - timer triggers overflow int when TCNT0 = OCR0A

    TCNT0 = 0;      // timer ct - gets reset to 0 when ISR fires

    OCR0A = 125;    // overflow ct

    // TIMSK0
    // 0000 00..    don't care
    // .... ..1.    OCIE0A compare match A interrupt enable
    // .... ...0    don't care
    TIMSK0 = 0b00000010;

    // TCCR0A
    // this sets CTC mode, and also activates timer (CS0x > 0)
    // 0000 ....    don't care
    // .... 1...    CTC0 - set to 1 for CTC mode
    // .... .010    CS02:CS00 - set to 1/8 prescale
    TCCR0A = 0b00001010;
}

// ---------------------------------------------------------

ISR(TIMER0_COMPA_vect)
{
    // 1 msec overflow - CTC mode
    static uint16_t counter = 0;    // keeps track of # of iterations thru ISR

    counter++;

    if (counter == 2)        // display refresh every 2 msec
    {
        bit_set(sys_flags, sf_change_digit);
        counter = 0;
    }
}

// ---------------------------------------------------------

// old code from ISR
/*switch (currDigit)
        {
            case 0:
                LED_PORT = ZERO;
                break;
            case 1:
                LED_PORT = ONE;
                break;
            case 2:
                LED_PORT = TWO;
                break;
            case 3:
                LED_PORT = THREE;
                break;
            case 4:
                LED_PORT = FOUR;
                break;
            case 5:
                LED_PORT = FIVE;
                break;
            case 6:
                LED_PORT = SIX;
                break;
            case 7:
                LED_PORT = SEVEN;
                break;
            case 8:
                LED_PORT = EIGHT;
                break;
            case 9:
                LED_PORT = NINE;
                break;
        }
        currDigit++;
        if (currDigit == 10)
            currDigit = 0;
        */


// ---------------------------------------------------------
